package com.jokesoft.supermanager.client;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jokesoft.supermanager.common.save.ClientSavePersonDataRequest;

public class HttpPostClient {

    private final static org.slf4j.Logger LOGGER = LoggerFactory.getLogger(HttpPostClient.class);

    private final String url;
    private HttpClient client;
    private HttpPost post;

    public void insertPerson(ClientSavePersonDataRequest person) throws JsonProcessingException {

        String personJson;

        ObjectMapper mapper = new ObjectMapper();

        personJson = mapper.writeValueAsString(person);

        System.out.println("Jason = " + personJson);
        LOGGER.debug("Json string = {}", personJson);

        post.setHeader("Accept", "application/json");
        post.setHeader("Content-Type", "application/json");

        try {
            post.setEntity(new StringEntity(personJson));
        } catch (UnsupportedEncodingException e) {
            // TODO Check these if better to delegate out
            e.printStackTrace();
        }

        try {
            HttpResponse response = client.execute(post);
            LOGGER.info("Return = {}", response.getStatusLine().toString());
            System.out.println("ReturnCode = " + response.getStatusLine().getStatusCode());
            System.out.println("Return = " + response.getStatusLine().toString());
            System.out.println("Return message = " + response.getEntity().getContent().toString());
        } catch (ClientProtocolException e) {
            // TODO Check these if better to delegate out
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Check these if better to delegate out
            e.printStackTrace();
        }

    }

    public static class Builder {

        private String host;
        private String port;
        private String path;

        public Builder host(String host) {
            this.host = host;
            return this;
        }

        public Builder path(String path) {
            this.path = path;
            return this;
        }

        public Builder port(String port) {
            this.port = port;
            return this;
        }

        public HttpPostClient build() {
            return new HttpPostClient(this);
        }

    }

    private HttpPostClient(Builder builder) {
        StringBuilder urlBuilder = new StringBuilder().append("http://")
                                                      .append(builder.host)
                                                      .append(":")
                                                      .append(builder.port)
                                                      .append(builder.path);
        this.url = urlBuilder.toString();
        this.client = HttpClientBuilder.create().build();
        this.post = new HttpPost(url);
    }

    @Override
    public String toString() {
        return "HttpPostClient [url=" + url + "]";
    }

}
